package cn.com.dhc.danlu.activity.delivery.order;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.delivery.order.adatper.MultiOrderRecyclerAdapter;
import cn.com.dhc.danlu.activity.delivery.order.delegate.IOrderListCallback;
import cn.com.dhc.danlu.activity.delivery.order.delegate.OrderListDelegate;
import cn.com.dhc.danlu.fragment.base.BaseV4Fragment;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.dhc.danlu.widget.DanluRecyclerView;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-02-13 15:15
 *
 * <p>
 * 我的订单 Fragment
 * </p>
 */
public class MyOrdersFragment extends BaseV4Fragment implements IOrderListCallback {
    private static final String KEY_POSITION = "position";
    /**
     * 订单状态
     */
    private String statusCode;

    private LoaddingLayout llLoading; // 加载视图
    private DanluRecyclerView drOrders;

    private OrderListDelegate delegate;
    private MultiOrderRecyclerAdapter adapter;

    private boolean isPrepare = false;

    protected static MyOrdersFragment newInstance(int position) {
        MyOrdersFragment fragment = new MyOrdersFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_POSITION, position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_orders, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initViews();
        initArgs();
        initApi();
        isPrepare = true;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isPrepare) {
            if (llLoading.isSuccessView()) {
                drOrders.scrollToPosition(0);
                drOrders.refresh();
            } else {
                llLoading.showLoaddingView();
            }
            callApiToLoad();
        }
    }

    private void initApi() {
        delegate = new OrderListDelegate(statusCode);
        delegate.attachHostView(this);
        drOrders.setRefreshView(delegate);
        delegate.setRefreshViewAttr(drOrders);
        adapter = new MultiOrderRecyclerAdapter();
        drOrders.setAdapter(adapter);
    }

    private void initArgs() {
        int position = getArguments().getInt(KEY_POSITION, 0);
        // 配送中心（销售）订单状态Code
        statusCode = getResources().getStringArray(R.array.my_order_status_code)[position];
    }

    private void initViews() {
        drOrders = (DanluRecyclerView) getView().findViewById(R.id.dr_orders);
        llLoading = (LoaddingLayout) getView().findViewById(R.id.ll_loading);
        llLoading.setRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llLoading.showLoaddingView();
                callApiToLoad();
            }
        });
    }

    @Override
    public void onSuccess(@NonNull List datas, boolean isRefresh) {
        if (isRefresh) {
            //下拉刷新回调
            adapter.refreshAllData(datas);
            drOrders.setLoadingMoreEnabled(true);
            if (llLoading.isSuccessView()) {
                // autorefresh | pullrefresh
                if (datas.isEmpty()) {
                    llLoading.showEmptyView();
                }
                drOrders.refreshComplete();
            } else {
                // click view mask | default
                if (datas.isEmpty()) {
                    llLoading.showEmptyView();
                } else {
                    llLoading.showContentView();
                }
            }
        } else {
            if (datas.isEmpty()) {
                ToastUtil.getInstance().show("没有更多数据");
                drOrders.setNoMore(true);
            } else {
                adapter.appendData(datas);
            }
            drOrders.loadMoreComplete();
        }
    }

    @Override
    public void onError(@NonNull String errorInfo, boolean isRefresh) {
        if (isRefresh) {
            drOrders.refreshComplete();
            // clear all
            adapter.refreshAllData(new ArrayList());
            llLoading.showErrorView(errorInfo);
        } else {
            ToastUtil.getInstance().show(errorInfo);
            drOrders.loadMoreComplete();
        }
    }

    /**
     * 请求接口，获取订单列表数据
     */
    private void callApiToLoad() {
        delegate.fetchData(true);
    }

    @Override
    public void onDestroy() {
        delegate.detachHostView();
        super.onDestroy();
    }
}
